﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NetBee.Entity.MySql;
using NetBee.Common;
namespace NetBeeManager.Controllers
{
    public class NetBeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        #region//配置
        #region//step 1.添加任务
        public ActionResult ShowAddTask()
        {
            using (var entity = new NetBeeEntities())
            {
                var tasks = entity.NetBee_Task.OrderByDescending(i => i.Id).Take(5).ToList();
                ViewBag.Tasks = tasks;
                if (tasks.Count > 0)
                {
                    ViewBag.NewTask = tasks.FirstOrDefault();
                }
                else
                {
                    ViewBag.NewTask = new NetBee_Task { Name = string.Empty };
                }
            }
            return View("/Views/NetBee/AddTask.cshtml");
        }
        public ActionResult AddTask(NetBee_Task task)
        {
            using (NetBeeEntities entity = new NetBeeEntities())
            {
                if (!string.IsNullOrEmpty(task.Name))
                {
                    //entity.NetBee_Task.Attach(task);
                    entity.NetBee_Task.Add(task);
                    entity.SaveChanges();
                }
                else
                {
                    Response.Write("<script type='text/javascript'>");
                    Response.Write("alert('任务名为空');");
                    Response.Write("</script>");
                    return View("/Views/NetBee/AddTask.cshtml");
                }
            }
            return RedirectToAction("ShowAddUrl/" + task.Id, "NetBee");
        }
        #endregion
        #region//step 2.添加url
        public ActionResult ShowAddUrl(int id)
        {
            ViewBag.TaskId = id;
            return View("/Views/NetBee/AddUrl.cshtml");
        }
        public ActionResult AddUrl(int id, string urlPattern, int count)
        {
            using (var entity = new NetBeeEntities())
            {
                var urls = new List<NetBee_Url>(count);
                for (int i = 1; i <= count; i++)
                {
                    var url = new NetBee_Url();
                    url.TaskId = id;
                    url.Url = string.Format(urlPattern, i);
                    url.Status = 200;
                    entity.NetBee_Url.Add(url);
                }
                entity.SaveChanges();
            }
            return RedirectToAction("ShowAddRule/" + id, "NetBee");
        }
        #endregion
        #region//step 3.添加匹配规则
        public ActionResult ShowAddRule(int id)
        {
            ViewBag.TaskId = id;
            return View("/Views/NetBee/AddRule.cshtml");
        }
        public ActionResult AddRule(List<NetBee_GatherRules> rules)
        {
            using (var entity = new NetBeeEntities())
            {
                foreach (var rule in rules)
                {
                    entity.NetBee_GatherRules.Add(rule);
                }
                entity.SaveChanges();
            }
            return RedirectToAction("ShowAddTask");
        }
        #endregion
        #endregion
        #region//查看数据
        #region//查看任务列表
        public ActionResult ListTasks(int index, int size)
        {
            #region//参数检查
            if (index < 1)
            {
                index = 1;
            }
            if (size < 1)
            {
                size = 10;
            }
            #endregion
            using (var entities = new NetBeeEntities())
            {
                int records = 0;
                #region//数据集合

                records = entities.NetBee_Task.Count();
                ViewBag.Tasks = entities.NetBee_Task.AsNoTracking().OrderByDescending(i => i.Id).Skip((index - 1) * size).Take(size).ToList();
                #endregion
                #region//分页
                ViewBag.Records = records;
                ViewBag.Pages = PageBuilder.LinkedPage(records, size, index, 10, "/NetBee/ListTasks/{index}/" + size, "{index}");
                ViewBag.PageCount = (int)System.Math.Ceiling(records / (decimal)size);
                #endregion
            }
            return View("/Views/NetBee/ListTasks.cshtml");
        }
        #endregion
        #endregion
    }
}
